/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 * 	  David Corbin - added method to parse depot path into individual elements
 */
package net.sourceforge.p4eclipse.core.resources;

import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;

import java.io.InputStream;

import net.sourceforge.p4eclipse.core.syncinfo.ResourceSyncInfo;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.sync.IRemoteResource;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
 * @author		<a href="mailto:dcorbin@users.sourceforge.net">David Corbin</a>
  */
public abstract class RemoteResource extends PlatformObject implements IRemoteResource
{
	private RemoteFolder parent;
	
	/**
	 * Gets the parent.
	 * @return Returns a RemoteFolder
	 */
	public RemoteFolder getParent()
	{
		return parent;
	}

	/**
	 * Sets the parent.
	 * @param parent The parent to set
	 */
	public void setParent(RemoteFolder parent)
	{
		this.parent = parent;
	}


	public static List splitRepositoryPath(String path)
		throws ParseException
	{
		List list = new LinkedList();
		if (!path.startsWith("//"))
			throw new ParseException("not a well-formed depot path",0);
			
		path = path.substring(2);
		if (path.startsWith("/"))
			throw new ParseException("not a well-formed depot path",2);
		
		while (path.length() > 0)
		{
			int slashPosition = path.indexOf('/');
			if (slashPosition < 0)
			{
				list.add(path);
				return list;
			}
			String fileName = path.substring(0,slashPosition);
			path = path.substring(slashPosition+1);
			list.add(fileName);
		}
		list.add("");
		return list;
	}


	/*
	 * @see IRemoteResource#getContents(IProgressMonitor)
	 */
	public abstract InputStream getContents(IProgressMonitor progress) 
		throws TeamException;

	/*
	 * @see IRemoteResource#getName()
	 */
	public abstract String getName();

	/*
	 * @see IRemoteResource#isContainer()
	 */
	public abstract boolean isContainer();

	/*
	 * @see IRemoteResource#members(IProgressMonitor)
	 */
	public abstract IRemoteResource[] members(IProgressMonitor progress)
		throws TeamException;

	public abstract ResourceSyncInfo getSyncInfo();
}
